<?php
/**
 * Created by PhpStorm.
 * User: 热心观众
 * Date: 2018/10/9
 * Time: 9:24
 */

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Center extends Controller
{
    // 个人中心首页
    public function Center()
    {
        // 接收Session 数据
        $userSession = Session::get('user');
         // 判断有无Session
        if (empty($userSession)) {
            $msg = "您还没有登录！";
            $url = "../log/login";
            return $this->error($msg,$url);
        }
        // 查询 用户信息
        $user = Db::table('zz_user')->where('zz_name',$userSession['name'])->find();
        // 查询 用户提问
        $userProblem = Db::table('zz_problem')->where('zz_uid',$user['zz_id'])->select();
        // 统计用户 提问个数
        $count = sizeof($userProblem);
        // 现在时间
        $time = Date('Y年m月');
        $d = Date('d');
        // 用户收藏问题
        $collect = Db::table('zz_collect')->where('zz_user_id',$user['zz_id'])->select();

        $collPro = [];
        // 不为空
        if (!empty($collect)){
            for ($i=0;$i<sizeof($collect);$i++){
                $collPro[] = Db::table('zz_problem')->where('zz_problem_id',$collect[$i]['zz_problem_id'])->find();
            }
        }

        // 关注
        $userFollow = Db::table('zz_follow')->where('zz_user_id',$user['zz_id'])->select();
        // 关注者 查询 被关注者信息 $followUserInf
        $followUserInf = [];
        if (!empty($userFollow)){
            for ($i=0;$i<sizeof($userFollow);$i++){
                $followUserInf[] = Db::table('zz_user')->where('zz_id',$userFollow[$i]['zz_concern_id'])->find();
            }
        }
        // 粉丝
        $followUser = Db::table('zz_follow')->where('zz_concern_id',$user['zz_id'])->select();
        // 查询粉丝 信息 $userFollowInf
        $userFollowInf = [];
        if (!empty($followUser)){
            for ($i=0;$i<sizeof($followUser);$i++){
                $userFollowInf[] = Db::table('zz_user')->where('zz_id',$followUser[$i]['zz_user_id'])->find();
            }
        }

        // 用户的评价
        $userComment = Db::table('zz_comment')->where('zz_comment_uid',$user['zz_id'])->select();
        // 查询相对应问题
        $up = [];
        for ($i=0;$i<sizeof($userComment);$i++){
            $up[] = Db::table('zz_problem')->where('zz_problem_id',$userComment[$i]['zz_problem_id'])->find();
        }
        // 嵌入
        for ($i=0;$i<sizeof($userComment);$i++){
            for ($j=0;$j<sizeof($up);$j++){
                if ($userComment[$i]['zz_problem_id'] == $up[$j]['zz_problem_id']){
                    $userComment[$i]['title'] = $up[$j]['zz_problem_tit'];
                }
            }
        }

        return view('index/center',[
            'user'=> $user,
            'userProblem' => $userProblem,
            'count' => $count,
            'time' => $time,
            'd' => $d,
            'collPro' => $collPro,
            'followUserInf' => $followUserInf,
            'userFollowInf' => $userFollowInf,
            'userComment' => $userComment
        ]);
    }

    //    个人资料
    public function UserData()
    {
        $userSession = Session::get('user');
        // 判断有无Session
        if (empty($userSession)) {
            $msg = "您还没有登录！";
            $url = "../log/login";
            return $this->error($msg,$url);
        }
        
        $user = Db::table('zz_user')->where('zz_name',$userSession['name'])->find();
        return view('index/userdata',['user'=>$user]);
    }


//    修改资料
    public function EditUser()
    {
        $data = input('post.');
        $se['name'] = $data['zz_name'];

        $res = Db::table('zz_user')->where('zz_id',$data['zz_id'])->update($data);
        if ($res){
            session('user',null);
            Session::set('user',$se);

            $this->success('修改成功');
        }else{
            $this->error('修改失败');exit;
        }

    }
    
    
//    修改头像
    public function Icon()
    {
//        获取文件
        $file = request()->file('icon');
        $id = input('zz_id');

//        判断是否选择文件
        if (empty($file)){
            $this->error('请选择头像...');
        }
//        移动文件到专属目录
        $info = $file->move(ROOT_PATH . 'public' . DS .'static//index//uploads');
//        定义文件格式
        $images = ['gif','jpg','jpeg','bmp','png'];
        if ( !in_array($info->getExtension(),$images)){
            $this->error('您的图片格式不正确...');
        }

        $data['zz_icon'] = $info->getSaveName();
        $res = Db::table('zz_user')->where('zz_id',$id)->update($data);

        if($res > 0 ){
            $this->success('修改成功, 正在为您跳转...');
        } else {
            $this->success('修改失败, 请稍后再试...');
        };

    }


    // 修改密码
    public function EditPass()
    {
        $userSession = Session::get('user');
        // 判断有无Session
        if (empty($userSession)) {
            $msg = "您还没有登录！";
            $url = "../log/login";
            return $this->error($msg,$url);
        }
        return view('index/editpass');
    }


    // 执行修改密码
    public function DoPass()
    {
        $data = input('post.');
        if (empty($data['pass'])){
            $this->error('原密码不能为空');exit;
        }
        if (empty($data['zz_pass'])){
            $this->error('新密码不能为空');exit;
        }
        if (empty($data['repass'])){
            $this->error('二次密码不能为空');exit;
        }
        $userSession = Session::get('user');
        $user = Db::table('zz_user')->where('zz_name',$userSession['name'])->find();
        if ($data['pass']!= $user['zz_pass']){
            $this->error('原密码输入有误');exit;
        }
        if ($data['zz_pass'] != $data['repass']){
            $this->error('两次密码不一致');exit;
        }
        unset($data['pass']);
        unset($data['repass']);

        $res = Db::table('zz_user')->where('zz_id',$user['zz_id'])->update($data);
        if ($res){
            //清空session
            session('user',null);
            $this->success('修改成功','/');
        }else{
            $this->error('修改失败');exit;
        }

    }



}